package com.weixing.mall.core.support.msg.ali;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.weixing.mall.core.support.msg.Msg;
import com.weixing.mall.core.support.msg.MsgApi;
import com.weixing.mall.core.support.msg.MsgResult;
import com.weixing.mall.core.support.msg.SmsParam;
import lombok.extern.slf4j.Slf4j;


/**
 * @Author: kfc
 * @Description: <br/>
 * Date:Create in 2019/5/29 16:28
 * @Modified By:
 */
@Slf4j
public class AliMsgApi implements MsgApi {

    private static String SIGN_NAME = "Lmagnese";
    private static String OK = "OK";

    /**
     * 发送短信
     *
     * @param msg
     */
    @Override
    public MsgResult sendMsg(Msg msg) {

        AliMsg aliMsg = (AliMsg) msg;

        DefaultProfile profile = DefaultProfile.getProfile("default", "LTAI4FuQg9dE9tBCgydWmzpR", "iXQZEv4TVnqO4CV1Imo5xYjrjUlrSG");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        //request.setProtocol(ProtocolType.HTTPS);
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("PhoneNumbers",aliMsg.getPhoneNo());
        request.putQueryParameter("SignName",SIGN_NAME);
        request.putQueryParameter("TemplateCode",aliMsg.getTemplate().getValue());
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(aliMsg.getParam()));
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            JSONObject object = (JSONObject) JSONObject.parse(response.getData());
            String code = object.getString("Code");
            String message = object.getString("Message");
            if (OK .equals(code)){
                return MsgResult.success(code,message);
            }else{
                return MsgResult.fail(code,message);
            }
        } catch (Exception e) {
            log.warn("阿里云短信发送异常:{}",msg.getPhoneNo(),e);
        }

        return MsgResult.fail();
    }

    /**
     * 根据业务类型处理对应的msg
     *
     * @param msg
     * @return
     */
    @Override
    public Msg handleBiz(Msg msg) {
        AliMsg aliMsg = initMsgBean(msg);
        switch (msg.getBizCode()){
            case REGISTER_USER:
                aliMsg.setTemplate(AliMsgTempEnum.REGISTER_USER);
                break;
            case RESET_PASSWORD:
                aliMsg.setTemplate(AliMsgTempEnum.RESET_PASSWORD);
                break;
            case LOGIN:
                aliMsg.setTemplate(AliMsgTempEnum.LOGIN);
                break;
        }
        return aliMsg;
    }

    private AliMsg initMsgBean(Msg msg){
        AliMsg aliMsg = new AliMsg();
        aliMsg.setBizCode(msg.getBizCode());
        aliMsg.setPhoneNo(msg.getPhoneNo());
        SmsParam param = msg.getParam();
        aliMsg.setParam(param);
        return aliMsg;
    }


}
